package com.tanhua.app.controllerservice;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.UserLocationApi;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.NearUserVo;
import com.tanhua.domain.vo.UserHolder;
import com.tanhua.domain.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @Reference
    UserLocationApi userLocationApi;

    @Reference
    UserInfoApi userInfoApi;

    public ResponseEntity location(Double longitude, Double latitude, String addrStr) {

        Long userId = UserHolder.getUser().getId();
        //存储用户的地理位置
        userLocationApi.location(longitude,latitude,addrStr,userId);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity search(String gender, Integer distance) {

        // 获取到自身距离半径的附近人
        List<UserLocationVo> userLocationVoList = userLocationApi.findNearUser(distance, UserHolder.getUser().getId());

        //查询附近人的详情（头像和昵称）
        List<NearUserVo> voList=new ArrayList();
        if(CollectionUtil.isNotEmpty(userLocationVoList)){
            for (UserLocationVo userLocationVo : userLocationVoList) {
                UserInfo userInfo = userInfoApi.findUserInfo(userLocationVo.getUserId());

                //排出自己
                if(UserHolder.getUser().getId()==userInfo.getId()){
                    continue;
                }
                //排出指定gender以外的性别
                if(!gender.equals(userInfo.getGender())){
                    continue;
                }
                //条件等等....

                //封装vo返回
                NearUserVo vo = new NearUserVo();
                vo.setUserId(userInfo.getId());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());

                voList.add(vo);
            }
        }
        return ResponseEntity.ok(voList);
    }
}
